home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / pmdev / c / demos / localemenu.c < prev    next >
C/C++ Source or Header  |  2000-02-28  |  3KB  |  134 lines

  1. /*
  2. // $VER: LocaleMenu.c 1.0 (23.08.99)
  3. //
  4. // Popup Menu example program
  5. //
  6. // ©1996-1997 Henrik Isaksson
  7. // All Rights Reserved.
  8. //
  9. // This example shows how to use the LocaleHook feature of
  10. // popupmenu.library.
  11. */
  12.  
  13. #include <intuition/intuition.h>
  14. #include <proto/intuition.h>
  15. #include <proto/exec.h>
  16. #include <clib/alib_protos.h>
  17. #include <stdio.h>
  18. #include <utility/hooks.h>
  19.  
  20. #include <libraries/pm.h>
  21. #include <proto/pm.h>
  22.  
  23. struct PopupMenuBase    *PopupMenuBase;
  24.  
  25. struct Window *OpenMyWindow(void);
  26.  
  27. #define    ID_QUIT        1    /* Use anything but 0 for the ID. */
  28. #define ID_ITEM        2
  29. #define ID_TITLE    3
  30.  
  31. char *strings[] = {
  32.     "Quit", "Menu Item", "Menu Title"
  33. };
  34.  
  35. STRPTR __asm __saveds GetStringFunc(register __a1 ULONG *args)
  36. {
  37.     return strings[*args-1];
  38.     
  39.     /* You should call the locale.library function */
  40.     /* GetCatalogStr() (or similar) here. */
  41.  
  42.     /* Example: */
  43.  
  44.     /* return GetCatalogStr(catalog, *args, strings[*args-1]); */
  45.  
  46.     /* (the last argument is the default string, which is */
  47.     /* returned if the ID was not fount in the catalog.) */
  48.     
  49. }
  50.  
  51. void main()
  52. {
  53.     BOOL            r=TRUE;    /* Keep running until this gets FALSE */
  54.     struct Window        *w;    /* One window. */
  55.     struct IntuiMessage    *im,imsg;
  56.     struct PopupMenu    *p;    /* One menu. */
  57.  
  58.     struct Hook        getstringhook;
  59.     
  60.     getstringhook.h_Entry = (HOOKFUNC) GetStringFunc;
  61.  
  62.     if(OPEN_PM_LIB) {    /* A little macro from libraries/pm.h. */
  63.         IntuitionBase=(struct IntuitionBase *)PopupMenuBase->pmb_IntuitionBase;
  64.  
  65.         /* Create a PopupMenu using the macros defined in pm.h: */
  66.  
  67.         p=PMMenuID(ID_TITLE),
  68.             PMItemID(ID_ITEM),            PMEnd,
  69.             PMBar,                    PMEnd,
  70.             PMItemID(ID_QUIT),    PM_UserData, 5,    PMEnd,
  71.           PMEnd;
  72.  
  73.         if(p) {
  74.             w=OpenMyWindow();    /* Open a little window. (declared below) */
  75.             if(w) {
  76.  
  77.                 /* A standard event loop follows: */
  78.                 while(r) {
  79.                     WaitPort(w->UserPort);
  80.                     while((im=(struct IntuiMessage *)GetMsg(w->UserPort))) {
  81.                         CopyMem(im,&imsg,sizeof(struct IntuiMessage));
  82.                         ReplyMsg((struct Message *)im);
  83.  
  84.                         switch(imsg.Class) {
  85.                             case IDCMP_CLOSEWINDOW: r=FALSE; break;
  86.                             case IDCMP_MOUSEBUTTONS:
  87.                                 r=PM_OpenPopupMenu(w,
  88.                                     PM_Menu,    p,
  89.                                     PM_LocaleHook,    &getstringhook,
  90.                                     TAG_DONE);
  91.  
  92.                                 /* If r is 5, we want to quit: */
  93.                                 
  94.                                 if(r==5) r=0;
  95.                                 else r=1;
  96.                                 
  97.                                 /* Maybe a bit confusing... Well, PM_OpenPopupMenu */
  98.                                 /* returns 0 if no item was select, so we can't just */
  99.                                 /* assign the return value to r. */
  100.                                 /* There is another (better?) way of handling the input; */
  101.                                 /* the PM_MenuHandler tag. */
  102.  
  103.                                 break;
  104.                         }
  105.                     }
  106.                 }
  107.                 CloseWindow(w);
  108.             } else printf("Window error!\n");
  109.             PM_FreePopupMenu(p);
  110.         } else printf("Menu error!\n");
  111.  
  112.         CLOSE_PM_LIB;
  113.     }
  114. }
  115.  
  116. struct Window *OpenMyWindow(void)
  117. {
  118.  
  119.     /* Open the window. I'm keeping this code here just to make the other */
  120.     /* code more readable. */
  121.  
  122.     return OpenWindowTags(NULL,
  123.             WA_IDCMP,    IDCMP_CLOSEWINDOW|IDCMP_MOUSEBUTTONS,    // Open a little window
  124.             WA_RMBTrap,    TRUE,
  125.             WA_DragBar,    TRUE,
  126.             WA_Width,    150,
  127.             WA_Height,    100,
  128.             WA_Left,    150,
  129.             WA_Top,        0,
  130.             WA_Title,    "LocaleHook",
  131.             WA_CloseGadget,    TRUE,
  132.         TAG_DONE);
  133. }
  134.